package edu.zueb.sport.rest;

import edu.zueb.sport.dao.EquipmentTypeMapper;
import edu.zueb.sport.entity.EquipmentType;
import edu.zueb.sport.entity.User;
import edu.zueb.sport.exception.RestException;
import edu.zueb.sport.rest.api.Page;
import edu.zueb.sport.rest.api.RestResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

@Tag(name = "器材类别管理", description = "器材类别管理")
@Slf4j
@RestController
@RequestMapping(EquipmentTypeRestController.PATH_EQUIPMENT_TYPE)
public class EquipmentTypeRestController {

    public static final String PATH_EQUIPMENT_TYPE = "/api/equipmentType";

    @Autowired
    EquipmentTypeMapper equipmentTypeMapper;

    @PostMapping(value = "/insert")
    public RestResponse<EquipmentType> insert(@RequestBody EquipmentType equipmentType, HttpServletRequest request) {
        // 这里可以用 HttpHolder 来获取，避免每次这样，懒得改了
        String token = request.getHeader("Authorization");
        User user = AuthRestController.TOKEN_CACHE.getIfPresent(token);
        if (user == null) {
            return RestResponse.fail("用户未登录");
        }
        try {
            equipmentType.setCreateUser(user.getUsername());
            equipmentType.setUpdateUser(user.getUsername());
            equipmentTypeMapper.insertEquipmentType(equipmentType);
        } catch (DuplicateKeyException e) {
            throw new RestException(equipmentType.getEquipmentTypeName() + "类别已存在");
        }
        return RestResponse.success(equipmentType);
    }

    @PostMapping(value = "/list")
    public RestResponse<Page<EquipmentType>> list(
            @RequestParam(value = "equipmentTypeName", required = false) String equipmentTypeName,
            @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        log.info("equipmentTypeName={}|pageNo={}|pageSize={}", equipmentTypeName, pageNo, pageSize);
        int totalCount = equipmentTypeMapper.countByPage(equipmentTypeName);
        log.info("totalCount={}", totalCount);
        if (totalCount == 0) {
            return RestResponse.success(Page.empty());
        }
        List<EquipmentType> equipmentTypes = equipmentTypeMapper.listByPage(equipmentTypeName, pageNo, pageSize);
        return RestResponse.success(Page.asPage(totalCount, pageSize, equipmentTypes));
    }

    @PostMapping(value = "/listAll")
    public RestResponse<List<EquipmentType>> list() {
        return RestResponse.success(equipmentTypeMapper.listAll());
    }

    @PostMapping(value = "/update")
    public RestResponse<EquipmentType> update(@RequestBody EquipmentType equipmentType, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        User user = AuthRestController.TOKEN_CACHE.getIfPresent(token);
        if (user == null) {
            return RestResponse.fail("用户未登录");
        }
        equipmentType.setUpdateUser("admin");
        int rows = equipmentTypeMapper.updateEquipmentType(equipmentType);
        if (rows != 1) {
            throw new RestException("更新失败");
        }
        return RestResponse.success(equipmentType);
    }

    @PostMapping(value = "/delete")
    public RestResponse<Boolean> delete(@RequestBody EquipmentType equipmentType) {
        int rows = equipmentTypeMapper.deleteEquipmentTypeById(equipmentType.getEquipmentTypeId());
        if (rows != 1) {
            throw new RestException("删除失败");
        }
        return RestResponse.success(true);
    }
}
